package com.ellfors.common.http.utils

import com.ellfors.common.ext.logI
import com.ellfors.common.ext.logJson


/**
 * HttpLogUtil
 * 2021-05-13 14:11
 */
object HttpLogUtil {

    private val TYEPS = arrayOf(
        "<-- END HTTP",
        "Alt-Svc:",
        "Access-Control-Allow-Origin:",
        "Access-Control-Expose-Headers:",
        "Connection:",
        "Content-Length:",
        "Content-Type:",
        "Cache-Control:",
        "Content-Security-Policy:",
        "Date:",
        "EagleId:",
        "Expires:",
        "Pragma:",
        "Timing-Allow-Origin:",
        "Transfer-Encoding:",
        "Server:",
        "Set-Cookie:",
        "Strict-Transport-Security:",
        "Vary:",
        "vary:",
        "Via:",
        "X-Cache:",
        "X-Swift-CacheTime:",
        "X-Swift-SaveTime:",
        "X-Kong-Upstream-Latency:",
        "X-Kong-Proxy-Latency:",
        "X-Content-Type-Options:",
        "X-XSS-Protection:",
        "X-Frame-Options:"
    )

    fun log(tag: String?, message: String) {
        //过滤掉无用信息
        if (!filterType(message))
            return
        if (message.startsWith("{") || message.startsWith("[")) {
            message.logJson(tag)
        } else {
            message.logI(tag)
        }
    }

    /**
     * 过滤类型
     */
    private fun filterType(message: String?): Boolean {
        if (message.isNullOrEmpty())
            return false
        for (str in TYEPS) {
            if (message.startsWith(str))
                return false
        }
        return true
    }
}